package cn.itcast.tramsformation;

import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author KTL
 * @version V1.0
 * @Package cn.itcast.tramsformation
 * @date 2021/2/24 0024 10:51
 * @Copyright © 2015-04-29  One for each, and two for each
 *              演示Transformations-rebalance-重平衡分区
 */
public class TransformationsDemo04 {
    public static void main(String[] args) throws Exception {
        //TODO 0.env
        final StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();
        environment.setRuntimeMode(RuntimeExecutionMode.AUTOMATIC);
        //TODO 1.source
        final DataStreamSource<Long> longDS = environment.fromSequence(0, 100);
        final SingleOutputStreamOperator<Long> filterDS = longDS.filter(new FilterFunction<Long>() {
            @Override
            public boolean filter(Long value) throws Exception {
                return value > 100;
            }
        });
        //TODO 2.trancefromat
        final SingleOutputStreamOperator<Tuple2<Integer, Integer>> result1 = filterDS.map(new RichMapFunction<Long, Tuple2<Integer, Integer>>() {
                 @Override
                 public Tuple2<Integer, Integer> map(Long value) throws Exception {
                     final int subTaskId = getRuntimeContext().getIndexOfThisSubtask();
                     return Tuple2.of(subTaskId, 1);
                 }
             }
        ).keyBy(t->t.f0).sum(1);
        //TODO 3.sink
        SingleOutputStreamOperator<Tuple2<Integer, Integer>> result2 = filterDS.rebalance()
                .map(new RichMapFunction<Long, Tuple2<Integer, Integer>>() {
                    @Override
                    public Tuple2<Integer, Integer> map(Long value) throws Exception {
                        int subTaskId = getRuntimeContext().getIndexOfThisSubtask();//子任务id/分区编号
                        return Tuple2.of(subTaskId, 1);
                    }
                    //按照子任务id/分区编号分组,并统计每个子任务/分区中有几个元素
                }).keyBy(t -> t.f0).sum(1);
        //TODO 4.excet
        result1.print();
        result2.print();
        environment.execute();
    }
}
